import { $each, $isEmptyArr } from '../index'
import { setSeriesConditionStyle, setSeriesConditionStyle1 } from './useSeriesStyle.js'
import { set } from 'lodash-es'
/*
 * 1.雷达2：除了雷达图2其他的雷达图不配置条件格式
 * 2.其他组件
 * 3.条形图
 * 4.气泡/飞线地图
 * 5.散点图
 */
// 其他图表条件格式转换样式
function handleEchartCondition(options) {
    const {
        seriesData,
        indexArrar,
        fieldName,
        isExistLegend,
        generateOption,
        style,
        currentLegend,
        fieldNameChainMap,
        chartTag,
        chartType
    } = options
    $each(seriesData, (info, seriesIndex) => {
        const serieskey = info.name
        const seriesValue = info.data
        // 颜色维度存在时, 多个seriesItem中的数据的原始字段名相同，需要区分indexArrar中获取的数据不能大于seriesItem中的data的长度
        let indexNeed = []
        // 当维度值存在颜色维度 并且当前遍历的原始字段名称为度量数据时
        const xAxisAndLegendFieldName = [].concat(
            Object.values(fieldNameChainMap['xAxis'] || {}),
            Object.values(fieldNameChainMap['legend'] || {})
        )
        const funnelChart = !Object.values(fieldNameChainMap['legend'] || {}).includes(fieldName) && chartType === 'funnel'
        if (isExistLegend && (!xAxisAndLegendFieldName.includes(fieldName) || funnelChart) && chartTag !== 'radar') {
            // 判断indexArrar中的每一个数据应小于data的长度 * (seriesIndex + 1),在indexNeed中存满足条件的下标数
            $each(indexArrar, indexItem => {
                if (indexItem < seriesValue.length * (seriesIndex + 1) && indexItem >= seriesValue.length * seriesIndex) {
                    indexNeed.push(indexItem % seriesValue.length)
                }
            })
        } else {
            indexNeed = indexArrar
        }
        /**
         * 允许进入情况：
         * 1、当前原始名称为维度和颜色维度
         * 2、颜色维度不存在时，度量有多度量情况
         * 3、颜色维度存在时，度量就一个值，可进去
         */
            // 度量的名称有多种情况
        const yAxisArr = ['yAxis', 'yAxisColor', 'yAxisLine', 'yAxisBar', 'xAxisScatter']
        // 将当前满足条件的度量原始名称保存起来
        let yAxisFieldNameMap = yAxisArr.map(item => fieldNameChainMap[item] && fieldNameChainMap[item][serieskey])
        // 散点图特殊处理
        if (fieldNameChainMap['xAxisScatter']) {
            yAxisFieldNameMap = yAxisFieldNameMap.concat(Object.values(fieldNameChainMap['xAxisScatter'] || {}))
        }
        // 雷达图需要单独判断度量  判断当前条件判断的是否是度量
        const currentYAxis =
            chartTag === 'radar' && (Object.values(fieldNameChainMap['yAxis'] || {}).includes(fieldName) || false)
        if (
            !$isEmptyArr(indexNeed) &&
            (xAxisAndLegendFieldName.includes(fieldName) || yAxisFieldNameMap.includes(fieldName) || isExistLegend)
        ) {
            setSeriesConditionStyle[chartTag]({
                seriesValue,
                generateOption,
                indexNeed,
                currentLegend,
                style,
                seriesIndex,
                currentYAxis
            })
        }
    })
}

function handleEchartCondition1(options) {
    const {
        seriesData,
        indexArrar,
        // fieldName,
        isExistLegend,
        // generateOption,
        // style,
        // currentLegend,
        fieldNameChainMap,
        chartTag,
        chartType
    } = options
    $each(seriesData, (info, seriesIndex) => {
        const serieskey = info.name
        const seriesValue = info.data
        // 颜色维度存在时, 多个seriesItem中的数据的原始字段名相同，需要区分indexArrar中获取的数据不能大于seriesItem中的data的长度
        let indexNeed = []
        // 当维度值存在颜色维度 并且当前遍历的原始字段名称为度量数据时
        const xAxisAndLegendFieldName = [].concat(
            Object.values(fieldNameChainMap['xAxis'] || {}),
            Object.values(fieldNameChainMap['legend'] || {})
        )

        /**
         * 允许进入情况：
         * 1、当前原始名称为维度和颜色维度
         * 2、颜色维度不存在时，度量有多度量情况
         * 3、颜色维度存在时，度量就一个值，可进去
         */
            // 度量的名称有多种情况
        const yAxisArr = ['yAxis', 'yAxisColor', 'yAxisLine', 'yAxisBar', 'xAxisScatter']
        // 将当前满足条件的度量原始名称保存起来
        let yAxisFieldNameMap = yAxisArr.map(item => fieldNameChainMap[item] && fieldNameChainMap[item][serieskey])
        // 散点图特殊处理
        if (fieldNameChainMap['xAxisScatter']) {
            yAxisFieldNameMap = yAxisFieldNameMap.concat(Object.values(fieldNameChainMap['xAxisScatter'] || {}))
        }

        $each(indexArrar, indexItem => {
            const { fieldName, valueIndex } = indexItem
            const funnelChart =
                !Object.values(fieldNameChainMap['legend'] || {}).includes(fieldName) && chartType === 'funnel'
            indexItem.currentYAxis =
                chartTag === 'radar' && (Object.values(fieldNameChainMap['yAxis'] || {}).includes(fieldName) || false)
            indexItem.isExecute =
                xAxisAndLegendFieldName.includes(fieldName) || yAxisFieldNameMap.includes(fieldName) || isExistLegend
            if (isExistLegend && (!xAxisAndLegendFieldName.includes(fieldName) || funnelChart) && chartTag !== 'radar') {
                // 判断indexArrar中的每一个数据应小于data的长度 * (seriesIndex + 1),在indexNeed中存满足条件的下标数
                if (valueIndex < seriesValue.length * (seriesIndex + 1) && valueIndex >= seriesValue.length * seriesIndex) {
                    indexNeed.push({
                        ...indexItem,
                        valueIndex: valueIndex % seriesValue.length
                    })
                }
            } else {
                indexNeed.push(indexItem)
            }
        })

        // TODO雷达图需要单独判断度量  判断当前条件判断的是否是度量
        // const currentYAxis = chartTag === 'radar' && (Object.values(fieldNameChainMap['yAxis'] || {}).includes(fieldName) || false);
        setSeriesConditionStyle1[chartTag]({
            seriesValue,
            // generateOption,
            indexNeed,
            // currentLegend,
            // style,
            seriesIndex
            // currentYAxis
        })
    })
}

// 条形图条件格式转换
function handleStripCondition(options) {
    const { seriesData, indexArrar, generateOption, style } = options
    seriesData.map((dataItem, dataIndex) => {
        for (const key in generateOption) {
            $each(indexArrar, item => {
                // 则统一设置属性 设置的颜色不为空才执行
                dataIndex === item && style[key] && set(dataItem, generateOption[key], style[key])
            })
        }
        return dataItem
    })
}

// 对比环形图条件格式转换
function handlePieDoubleCondition(options) {
    const { seriesData, indexArrar, fieldName, generateOption, style, fieldNameChainMap } = options
    $each(seriesData, (info, seriesIndex) => {
        const seriesValue = info.data
        const { yAxis } = fieldNameChainMap
        let indexNeed = []
        Object.keys(yAxis).forEach((item, index) => {
            if (yAxis[item] === fieldName) {
                indexNeed = [index]
            }
        })
        if (!$isEmptyArr(indexArrar)) {
            seriesValue.map((dataItem, dataIndex) => {
                for (const key in generateOption) {
                    $each(indexNeed, item => {
                        // 则统一设置属性 设置的颜色不为空才执行
                        dataIndex === item && style[key] && set(dataItem, generateOption[key], style[key])
                    })
                }
                return dataItem
            })
        }
    })
}

// 气泡、飞线地图
function handleScatterMapCondition(options) {
    const { seriesData, indexArrar, fieldName, generateOption, style, currentLegend, fieldNameChainMap, chartTag } =
        options
    $each(seriesData, (seriesValue, seriesIndex) => {
        const xAxisAndLegendFieldName = [].concat(Object.values(fieldNameChainMap['xAxis'] || {}))
        const indexNeed = indexArrar
        const yAxisArr = seriesValue.fieldName
        // 气泡地图条件格式为维度值时，只需要控制气泡
        if (
            !$isEmptyArr(indexNeed) &&
            ((xAxisAndLegendFieldName.includes(fieldName) && seriesIndex === 0) || yAxisArr.includes(fieldName))
        ) {
            setSeriesConditionStyle[chartTag]({
                seriesValue,
                generateOption,
                indexNeed,
                currentLegend,
                style
            })
        }
    })
}

// 雷达图2 条件格式转换
function handleRadarTwoCondition(options) {
    const { seriesData, indexArrar, fieldName, generateOption, style, currentLegend, fieldNameChainMap } = options
    const xAxisFieldName = [].concat(Object.values(fieldNameChainMap['xAxis'] || {}))
    const yAxisFieldName = [].concat(Object.values(fieldNameChainMap['yAxis'] || {}))
    const legendFieldName = [].concat(Object.values(fieldNameChainMap['legend'] || {}))
    // 每组数据的长度
    const seriesItemValueLength = seriesData[0].data[0].value.length
    // 对应颜色维度有几组
    const xAxisDataNumber = seriesData.length / (seriesItemValueLength + 1)
    // 获取雷达图2度量数据同个原始字段名的首个下标值
    const radarFirstIndex = seriesData.findIndex(item => item.fieldName === fieldName && item.z === 1) || 0
    seriesData.map((seriesItem, seriesIndex) => {
        const indexNeed = indexArrar
        // 雷达图2
        if (!$isEmptyArr(indexNeed)) {
            for (const key in generateOption) {
                $each(indexNeed, item => {
                    // 当原始字段名与维度数据的原始字段名称相同时
                    if (xAxisFieldName.includes(fieldName)) {
                        seriesIndex === item + 1 &&
                        style[key] &&
                        seriesItem.z === 2 &&
                        set(seriesItem, generateOption[key], style[key])
                        // 当series中的数据大于维度的数据长度+1  多度量或者有颜色维度时
                        if (seriesData.length > seriesItemValueLength + 1) {
                            for (let i = 0; i < xAxisDataNumber; i++) {
                                seriesIndex === item + 1 + i * (seriesItemValueLength + 1) &&
                                style[key] &&
                                seriesItem.z === 2 &&
                                set(seriesItem, generateOption[key], style[key])
                            }
                        }
                    } else if (currentLegend) {
                        // 当前颜色维度
                        for (let i = 0; i < xAxisDataNumber; i++) {
                            seriesIndex === item * xAxisDataNumber + i &&
                            style[key] &&
                            seriesItem.z === 2 &&
                            set(seriesItem, generateOption[key], style[key])
                        }
                    } else if (yAxisFieldName.includes(fieldName)) {
                        // 当前度量
                        if (legendFieldName.length) {
                            // 颜色维度存在
                            seriesIndex === parseInt(item / seriesItemValueLength) + item + 1 &&
                            style[key] &&
                            seriesItem.z === 2 &&
                            set(seriesItem, generateOption[key], style[key])
                        } else {
                            // 则统一设置属性 设置的颜色不为空才执行
                            seriesIndex === item + 1 + radarFirstIndex &&
                            style[key] &&
                            seriesItem.z === 2 &&
                            set(seriesItem, generateOption[key], style[key])
                        }
                    }
                })
            }
        }
    })
}
export const handleDealCondition = {
    handleEchartCondition,
    handleStripCondition,
    handlePieDoubleCondition,
    handleScatterMapCondition,
    handleRadarTwoCondition,
    handleEchartCondition1
}

// 获取条件策略类型值
export function checkConditionStrategy(name) {
    let type = ''
    switch (name) {
        case 'strip':
            type = 'handleStripCondition'
            break
        case 'pieDouble':
            type = 'handlePieDoubleCondition'
            break
        case 'scatter':
            type = 'handleEchartCondition'
            break
        case 'scatterMap':
            type = 'handleScatterMapCondition'
            break
        case 'radarTwo':
            type = 'handleRadarTwoCondition'
            break
        default:
            type = 'handleEchartCondition'
            break
    }
    return type
}

/**
 * 生成唯一ID 可设置随机ID前缀及之间连接符
 *
 * @export
 * @param {string} [prefix]
 * @param {string} [type]
 * @return {*}  {string}
 */
export function createUniqueId(prefix, type = '_') {
    let uniqueId = ''
    if (prefix) {
        uniqueId = prefix + type
    }
    return uniqueId + nanoid()
}
